<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
      <el-row style="height:50px;">
        <el-button style="float:right;" type="primary" size="mini" @click="editHandle">点击更改用户信息</el-button>
      </el-row>
      <el-form-item label="头像">
        <UploadFile ref="uploadRef" :photo-list="photoList" :objid="form.studentname" :objtype="'user'" @changeFileList="changeFileList" />
      </el-form-item>
      <el-form-item label="姓名" prop="studentname">
        <el-input v-model="form.studentname" :readonly="!isEdit" />
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="form.sex" :disabled="!isEdit">
          <el-radio label="1">男</el-radio>
          <el-radio label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="form.phone" :readonly="!isEdit" />
      </el-form-item>
      <el-form-item label="学号" prop="studentid">
        <el-input v-model="form.studentid" :readonly="!isEdit" />
      </el-form-item>
      <el-form-item v-if="isEdit" label="学院班级">
        <el-cascader
          v-model="label"
          :options="options"
          :props="{ expandTrigger: 'hover' }"
          @change="handleChange"
        />
      </el-form-item>
      <el-form-item v-else label="学院班级">
        <el-input v-model="form.college" readonly />
        <el-input v-model="form.classname" readonly />
      </el-form-item>
      <el-form-item label="个人评价(选填)" prop="detail">
        <el-input v-model="form.detail" :readonly="!isEdit" />
      </el-form-item>
      <el-form-item>
        <el-button :disabled="!isEdit" type="primary" @click="onSubmit">修改</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
import { getInfo, editUser } from '@/api/user'

export default {
  data() {
    return {
      isEdit: false,
      userId: this.$route.params.id,
      fileList: [],
      photoList: [],
      form: {
        studentname: '',
        sex: '1',
        phone: '',
        studentid: '',
        is_delete: 0,
        detail: '',
        college: '',
        classname: ''
      },
      rules: {
        studentname: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '电话不能为空', trigger: 'blur' },
          { min: 10, max: 15, message: '长度在 10 到 15 个字符', trigger: 'blur' }
        ],
        studentid: [
          { required: true, message: '学号不能为空', trigger: 'blur' },
          { min: 9, max: 12, message: '长度在 9 到 12 个字符', trigger: 'blur' }
        ]
      },
      label: [],
      options: [{
        value: '软件学院',
        label: '软件学院',
        children: [{
          value: '物联网班',
          label: '物联网班'
        }, {
          value: '软件工程班',
          label: '软件工程班'
        }]
      },
      {
        value: '文传院',
        label: '文传院',
        children: [{
          value: '播音班',
          label: '播音班'
        }, {
          value: '主持班',
          label: '主持班'
        }, {
          value: '新媒体班',
          label: '新媒体班'
        }]
      },
      {
        value: '机电院',
        label: '机电院',
        children: [{
          value: '机械班',
          label: '机械班'
        }, {
          value: '机器人班',
          label: '机器人班'
        }]
      },
      {
        value: '音乐与舞蹈学院',
        label: '音乐与舞蹈学院',
        children: [{
          value: '声乐班',
          label: '声乐班'
        }, {
          value: '舞蹈班',
          label: '舞蹈班'
        }]
      },
      {
        value: '美术学院',
        label: '美术学院',
        children: [{
          value: '美教班',
          label: '美教班'
        }, {
          value: '艺术班',
          label: '艺术班'
        }]
      }]
    }
  },
  created() {
    this.getUserDetailById()
  },
  methods: {
    async getUserDetailById() {
      const data = await getInfo(this.userId)
      // console.log(data)
      this.form = data
      // const form = this.form
      // for (const key in data) {
      //   if (form.hasOwnProperty.call(key)) {
      //     form[key] = data[key]
      //   }
      // }
      this.form.sex = this.form.sex.toString()
      this.form.phone = this.form.phone.toString()
      // this.form = form
      this.$refs.uploadRef.getObjImg(this.form.studentname)
    },
    changeFileList(val) {
      this.photoList = val
    },
    onSubmit() {
      this.$refs.formRef.validate(isOk => {
        if (isOk) {
          this.$confirm('确定要更改该用户信息吗').then(() => {
            // 如果点击了确定就会进入then
            return editUser(this.userId, this.form) // 返回promise对象
          }).then(() => {
            this.$message.success('更改成功')
            this.getUserDetailById()
            this.fileList = []
            this.isEdit = false
          })
        }
      })
    },
    handleChange() {
      this.form.college = this.label[0]
      this.form.classname = this.label[1]
    },
    editHandle() {
      this.isEdit = !this.isEdit
    }
  }
}
</script>
<style scoped>
/* .infoavatar{
  width: 100px;
  height: 100px;
} */
</style>
